package com.fang.transfer.file.listener;

import com.fang.transfer.file.ThreadContext;
import com.fang.transfer.file.common.code.ClientSendCode;
import com.fang.transfer.file.common.NettyHandle;
import com.fang.transfer.file.common.RequestListener;
import com.fang.transfer.file.common.code.ServiceSendCode;
import com.fang.transfer.file.common.model.TransferFileRequest;
import com.fang.transfer.file.common.model.User;
import com.fang.transfer.file.service.FileSendRequestService;
import com.fang.transfer.file.service.UserService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 按照下标传输
 */
@Service
@Slf4j
public class SendDataByIndexListener extends RequestListener<TransferFileRequest> {


    @Autowired
    private NettyHandle nettyHandle;
    @Autowired
    private FileSendRequestService fileSendRequestService;

    @Override
    public void handle(TransferFileRequest param) {
        TransferFileRequest request = fileSendRequestService.getById(param.getId());
        User sendUser = request.getSendUser();
        log.info("传输文件id:{}  index:{}", request.getId(), param.getReSendIndex());
        nettyHandle.send(sendUser.getId(), ServiceSendCode.SEND_FILE_DATA_INDEX, param);
    }

    @Override
    public String getCode() {
        return ClientSendCode.RE_SEND_FILE_DATA_INDEX;
    }

}
